package com.example.b_308.medical;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.b_308.medical.controller.UserController;
import com.example.b_308.medical.model.Staff;
import com.example.b_308.medical.view.account.IAccountView;
import com.example.b_308.medical.view.base.BaseActivity;
import com.example.b_308.medical.view.user.IUserView;

/**
 * Created by B-308 on 2017/11/22.
 */
public class Set_new_passwordActivity extends BaseActivity implements View.OnClickListener,IUserView{
    EditText et_phone_code2, et_password3, et_password4;
    ImageView iv_return, iv_phone_code_delete2, iv_password3_delete, iv_password4_delete;
    Button but_getCode_again,but_set_new_password;
    TextView tv_title,tv_setting;
    UserController userController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_new_password);
        initView();
        initDate();
        initListener();
    }

    private void initListener() {
        iv_return.setOnClickListener(this);
        iv_phone_code_delete2.setOnClickListener(this);
        iv_password3_delete.setOnClickListener(this);
        iv_password4_delete.setOnClickListener(this);
        but_getCode_again.setOnClickListener(this);
        but_set_new_password.setOnClickListener(this);
        et_phone_code2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_phone_code_delete2.setVisibility(View.GONE);
                } else {
                    iv_phone_code_delete2.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_password3_delete.setVisibility(View.GONE);
                } else {
                    iv_password3_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        et_password4.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 0) {
                    iv_password4_delete.setVisibility(View.GONE);
                } else {
                    iv_password4_delete.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initDate() {
        userController = new UserController(this);
    }


    private void initView() {
        iv_return = findView(R.id.iv_return);
        tv_title = findView(R.id.tv_title);
        tv_title.setText("设置新密码");
        tv_setting = findView(R.id.tv_setting);
        tv_setting.setVisibility(View.GONE);
        et_phone_code2 = findView(R.id.et_phone_code2);
        et_password3 = findView(R.id.et_password3);
        et_password4 = findView(R.id.et_password4);
        iv_phone_code_delete2 = findView(R.id.iv_phone_code_delete2);
        iv_password3_delete = findView(R.id.iv_password3_delete);
        iv_password4_delete = findView(R.id.iv_password4_delete);
        but_getCode_again = findView(R.id.but_getCode_again);
        but_set_new_password = findView(R.id.but_set_new_password);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_return:
                tent2(Verification_codeActivity.class);
                break;
            case R.id.iv_phone_code_delete2:
                et_phone_code2.setText("");
                break;
            case R.id.iv_password3_delete:
                et_password3.setText("");
                break;
            case R.id.iv_password4_delete:
                et_password4.setText("");
                break;
            case R.id.but_getCode_again:
                runOnBackGround(new Runnable() {
                    @Override
                    public void run() {

                    }
                });
                break;
            case R.id.but_set_new_password:
                userController.forgetpwd(et_phone_code2.getText().toString(),et_password3.getText().toString(),et_password4.getText().toString());
                break;
        }
    }



    @Override
    public void loginSuccess(Staff staff) {

    }

    @Override
    public void registerSuccess() {

    }

    @Override
    public void getCodeSuccess() {

    }

    @Override
    public void forgetpwdSuccess() {
        tent2(LoginActivity.class);
    }

    public void runOnBackGround(final Runnable runnable){
        new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] params) {
                new  Thread(){

                    int timer=60;
                    @Override
                    public void run() {
                        while (timer>=0){
                            timer-=1;
                            try {
                                Thread.sleep(1000);
                            }catch (InterruptedException e){
                                e.printStackTrace();
                            }
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (timer>0){
                                        but_getCode_again.setText(""+timer+"秒");
                                        but_getCode_again.setClickable(false);
                                        but_getCode_again.setSelected(true);
                                    }else {
                                        but_getCode_again.setText("重新获取");
                                        but_getCode_again.setClickable(true);
                                        but_getCode_again.setSelected(false);
                                    }
                                }
                            });
                        }
                    }

                }.run();

                return null;
            }
        }.execute();
    }
}
